/**
 * Created by FreezeSoul on 2018/10/13.
 */
define([
    "app/core/base",
    "app/core/util",
    "jquery",
    "underscore",
    'loglevel'
], function (Chart, Util, $, _, log) {

    var property = {
        config: "property",
        option: [
            {
                group: '基础',
                id: 'LoadedPageId',
                name: '页面选择',
                enable: true,
                type: 'pageSelect',
                link: 'pageId',
                value: '',
                default: '',
                tooltip: '选择加载页面'
            },
            {
                group: '基础',
                id: 'LoadedRootPageUrl',
                name: '页面根目录 ',
                enable: false,
                type: 'text',
                value: '/#/dashboard/include/',
                default: '/#/dashboard/include/',
                link: 'rootPath',
                tooltip: '页面根目录'
            },
            {
                group: '基础',
                id: 'PageIframeScroll',
                name: '显示滚动条 ',
                enable: false,
                type: 'checked',
                value: false,
                default: false,
                link: 'scroll',
                tooltip: '是否显示滚动条'
            }
        ],
        MapOption: function (option) {
            Util.linkOption(this.option, option);
        }
    };

    var label = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._element = null;
            this._container = null;
            this._option = {
                Series: [],
                Events: [],
                Extend: $.extend(true, {}, property),
                Option: {
                    uuid: '',
                    class: '',
                    display: true,
                    pageId: '',
                    scroll: false,
                    rootPath: '/#/dashboard/include/',
                    __beforeInitScript: '',
                    __afterInitScript: '',
                    __beforeSetOptionScript: '',
                    __afterSetOptionScript: '',
                    __beforeSetDataScript: '',
                    __afterSetDataScript: '',
                    __beforeGetOptionScript: '',
                    __afterGetOptionScript: '',
                    __beforeDisposeScript: '',
                    __afterDisposeScript: ''
                }
            };
        },
        init: function (element) {
            try {
                this._container = element;
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                Util.deepMapOption(property, this._option.Extend, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        _render: function (option) {
            $(this._container).empty();
            var url = option.rootPath + option.pageId;
            var $iframe = $('<iframe src="" width="100%" height="100%" frameborder="0"></iframe>');
            $iframe.attr("src", url + "?v=" + window.version).attr("scrolling", option.scroll ? "" : "no").appendTo(this._container);
            var timeOutId = setTimeout(function () {
                $iframe.css("opacity", "1");
            }, 5000);
            $iframe.css("opacity", "0").load(function () {
                $(this).css("opacity", "1");
                clearTimeout(timeOutId);
            });

            //Util.initChartLifeTime(this, this._container, this._option.Option);

            return $iframe;
        },
        example: function () {
            try {
                this._element = this._render(this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        setData: function (data) {
            try {
                this._readProperty();
                this._element = this._render(this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            try {
                this._readProperty();
            } catch (e) {
                log.error(e);
            }
            return this._option;
        },
        setTheme: function (theme) {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                $(this._element).off().remove();
                //Util.disposeChartLifeTime(this, this._container, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        getElement: function () {
            return this._element;
        },
        getChart: function () {
            return this._element;
        }
    });

    return {
        Chart: label,
        Property: property
    };
});